package com.mine.frame.common.utils

import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.mine.frame.common.R

/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/11/12
 * Description:Fragment显示隐藏工具类
 */
class FgmUtils {


    companion object {

        /**
         * Activity 中显示Fgm
         * 无动画
         * @param fgmManager   fgm管理器
         * @param showFragment 需要显示的Fragment
         * @param hideFragment 需要隐藏的Fragment
         * @param containerId  Fragment的容器
         * @param tag          当前显示的Fragment的Tag
         */
        fun showFgmInAtv(
            fgmManager: FragmentManager,
            showFragment: Fragment?,
            hideFragment: Fragment?,
            @IdRes containerId: Int,
            tag: String
        ) {
            if (showFragment == null || showFragment === hideFragment) {
                return
            }
            val ft = fgmManager.beginTransaction()
            hideFragment?.let {
                ft.hide(it)
            }
            if (showFragment.isAdded) {
                ft.show(showFragment)
            } else {
                ft.add(containerId, showFragment, tag)
            }
            ft.commit()
        }


        /**
         * Activity 中显示Fgm
         *  带左右滑动的动画
         * @param fgmManager   fgm管理器
         * @param showFragment 需要显示的Fragment
         * @param hideFragment 需要隐藏的Fragment
         * @param containerId  Fragment的容器
         * @param tag          当前显示的Fragment的Tag
         * @param animType     1是右边进入，0是左边进入
         */
        private fun showFgmAnimInAtv(
            fgmManager: FragmentManager,
            showFragment: Fragment?,
            hideFragment: Fragment?,
            containerId: Int,
            tag: String,
            animType: Int
        ) {
            if (showFragment == null || showFragment === hideFragment) {
                return
            }
            val ft = fgmManager.beginTransaction()
            if (animType > 0) {
                ft.setCustomAnimations(
                    R.anim.slide_right_in,
                    R.anim.slide_left_out,
                    R.anim.slide_left_in,
                    R.anim.slide_right_out
                )
            } else {
                ft.setCustomAnimations(
                    R.anim.slide_left_in,
                    R.anim.slide_right_out,
                    R.anim.slide_right_in,
                    R.anim.slide_left_out
                )
            }
            hideFragment?.let {
                ft.hide(it)
            }
            if (showFragment.isAdded) {
                ft.show(showFragment)
            } else {
                ft.add(containerId, showFragment, tag)
            }
            ft.commit()
        }


        /**
         * 子Fgm显示
         * Fgm里面嵌套Fgm
         * @param childFgmManager Fragment().getChildFragmentManager()
         * @param showFragment
         * @param containerId
         * @param tag
         */
        fun showChildFgm(
            childFgmManager: FragmentManager,
            showFragment: Fragment,
            containerId: Int,
            tag: String
        ) {
            val ft = childFgmManager.beginTransaction()
            if (showFragment.isAdded) {
                ft.show(showFragment)
            } else {
                ft.add(containerId, showFragment, tag)
            }
            ft.commit()
        }


        /**
         * 子Fgm隐藏
         *
         * @param hideFragment
         */
        fun hideChildFgm(childFgmManager: FragmentManager, hideFragment: Fragment?) {
            val ft = childFgmManager.beginTransaction()
            if (hideFragment == null) {
                return
            }
            if (hideFragment.isAdded) {
                ft.hide(hideFragment)
            }
            ft.commit()
        }

    }
}